package c1725163225.mzxy.androistest.androidtest;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import c1725163225.mzxy.androistest.androidtest.model.TopModel;
import c1725163225.mzxy.androistest.androidtest.model.WebViewModel;

public class DetailActivity extends AppCompatActivity {
ImageView mBackimg,mtitleimg;
TextView mVotetv,mCommenttv,mtitleimgtv;
WebView mWebView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        mVotetv=findViewById(R.id.votetv);
        mWebView=findViewById(R.id.webview);
        mCommenttv=findViewById(R.id.commenttv);
        mtitleimgtv=findViewById(R.id.titleimgtv);
        mtitleimg=findViewById(R.id.titleimg);
        mBackimg=findViewById(R.id.backimg);
        mBackimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Intent intent=getIntent();
        String id=intent.getStringExtra("id");
        String url="https://news-at.zhihu.com/api/4/news/"+id;
        RequestParams requestParams=new RequestParams(url);
        x.http().get(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.i("Detail",result);
                Gson gson=new Gson();
                WebViewModel webViewModel=gson.fromJson(result,WebViewModel.class);
       x.image().bind(mtitleimg,webViewModel.image);
                mtitleimgtv.setText(webViewModel.title);
                mWebView.loadDataWithBaseURL(null,webViewModel.body,"text/html","utf-8",null);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i("Detail",ex.getMessage());

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
        String url1="https://news-at.zhihu.com/api/4/story-extra/"+id;
        RequestParams requestParams1=new RequestParams(url1);
        x.http().get(requestParams1, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.i("Detail1",result);
                Gson gson=new Gson();
                TopModel topModel=gson.fromJson(result,TopModel.class);

                mVotetv.setText(topModel.popularity);
                mCommenttv.setText(topModel.comments);

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i("Detail1",ex.getMessage());

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }
}
